package com.market.service.impl;

import org.apache.commons.lang3.StringUtils;
import com.market.Dto.GoodsSalesDTO;
import com.market.Vo.OrderReportVO;
import com.market.Vo.SalesTop10ReportVO;
import com.market.Vo.TurnoverReportVO;
import com.market.Vo.UserReportVO;
import com.market.entity.Orders;
import com.market.mapper.OrderMapper;
import com.market.mapper.UserMapper;
import com.market.service.ReportService;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 特定区间营业额统计
     * @param begin
     * @param end
     * @return
     */
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        //用datelist储存日期
        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }
        //用turnoverList储存营业额
        List<Double> turnoverList=new ArrayList<>();

        for(LocalDate date:dateList){
            LocalDateTime beginTime=LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(date,LocalTime.MAX);

            Map map=new HashMap<>();
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("status", Orders.COMPLETED);

            Double sum = orderMapper.sumByMap(map);
            sum = sum == null ? 0.0 : sum;

            turnoverList.add(sum);
        }

        TurnoverReportVO turnoverReportVO=TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .turnoverList(StringUtils.join(turnoverList, ","))
                .build();
        return turnoverReportVO;
    }

    /**
     * 特定区间用户统计
     * @param begin
     * @param end
     */
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {

        //用datelist储存日期
        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }

        //新增用户用newUserList统计
        List<Integer> newUserList=new ArrayList<>();
        //总用户用totalUserList统计
        List<Integer> totalUserList=new ArrayList<>();

        for(LocalDate date:dateList){
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            Map map=new HashMap<>();
            map.put("end",endTime);
            //总的用户
            Integer totalUser=userMapper.countByMap(map);
            map.put("begin",beginTime);

            //新增用户
            Integer newUser=userMapper.countByMap(map);

            newUserList.add(newUser);
            totalUserList.add(totalUser);

        }
        return UserReportVO
                .builder()
                .dateList(StringUtils.join(dateList, ","))
                .newUserList(StringUtils.join(newUserList, ","))
                .totalUserList(StringUtils.join(totalUserList, ","))
                .build();
    }

    /**
     * 特定区间订单查询
     * @param begin
     * @param end
     * @return
     */
    public OrderReportVO getOrdersStatistics(LocalDate begin, LocalDate end) {
        //用datelist储存日期List
        List<LocalDate> dateList=new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)){
            begin=begin.plusDays(1);
            dateList.add(begin);
        }

        //储存每天订单总数
        List<Integer> orderCountList =new ArrayList<>();
        //储存每天有效订单数
        List<Integer> validOrderCountList =new ArrayList<>();

        //特定时间内订单总数
        Integer totalOrderCount=0;
        //特定时间内有效订单数
        Integer validOrderCount=0;

        for (LocalDate date:dateList){
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            Map map=new HashMap<>();
            map.put("end",endTime);
            map.put("begin",beginTime);
            Integer totalOrderCountnow = orderMapper.countByMap(map);
            totalOrderCount += orderMapper.countByMap(map);
            map.put("status",5);
            Integer validOrderCountnow=orderMapper.countByMap(map);
            validOrderCount+=orderMapper.countByMap(map);
           orderCountList.add(totalOrderCountnow);
           validOrderCountList.add(validOrderCountnow);
        }

        Double orderCompletionRate=0.0;
        if((double)validOrderCount!=0){
            orderCompletionRate=validOrderCount/(double)validOrderCount;
        }
        //封装数据
        return OrderReportVO
                .builder()
                .dateList(StringUtils.join(dateList, ""))
                .orderCountList(StringUtils.join(orderCountList, ","))
                .validOrderCountList(StringUtils.join(validOrderCountList, ","))
                .build();
    }

    /**
     * 特定区间销量top10
     * @param begin
     * @param end
     * @return
     */
    public SalesTop10ReportVO getTop10(LocalDate  begin, LocalDate end) {

        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

       List<GoodsSalesDTO> salesTop10=orderMapper.getTop10(beginTime,endTime);
        List<String> names = salesTop10.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        String nameList = StringUtils.join(names, ",");

        List<Integer> numbers = salesTop10.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        String numberList = StringUtils.join(numbers, ",");


        return SalesTop10ReportVO
                .builder()
                .nameList(nameList)
                .numberList(numberList)
                .build();
    }

}
